package net.bblot.lottery.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ApplicationInfo;

import net.bblot.lottery.PackegeConfig;

import java.io.File;

public class AppConfig {

	/** 存储当前的一些信息 */
	public static final String PREFERENCE_CONFIG_CURRENT_USER = "config_all";

	public static boolean getFirstUsage(Context context) {
		// TODO Auto-generated method stub
		SharedPreferences sp = getSharedPreferences(context);
		boolean firstUsage = false;
		try {
			PackageManager pm = context.getPackageManager();
			String appVersionName = pm.getPackageInfo(context.getPackageName(), 0).versionName + "";
			String version = sp.getString("firstUsage_version", "");
			if (!version.equals(appVersionName)) {
				firstUsage = true;
			}
		} catch (PackageManager.NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return firstUsage;

		// return true;
	}

	public static void saveFirstUsage(Context context, boolean firstUsage) {
		// TODO Auto-generated method stub
		SharedPreferences sp = context.getSharedPreferences(PREFERENCE_CONFIG_CURRENT_USER, Activity.MODE_PRIVATE);
		try {
			PackageManager pm = context.getPackageManager();
			String appVersionName = pm.getPackageInfo(context.getPackageName(), 0).versionName + "";
			sp.edit().putString("firstUsage_version", appVersionName).commit();
		} catch (PackageManager.NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 获取Preference设置
	 */
	private static SharedPreferences getSharedPreferences(Context context) {
		SharedPreferences sp = context.getSharedPreferences(PREFERENCE_CONFIG_CURRENT_USER, Activity.MODE_PRIVATE);
		return sp;
	}

	public static void saveDownLoadReference(Context context, String version, long reference, String filePath) {
		SharedPreferences sp = getSharedPreferences(context);
		SharedPreferences.Editor edt = sp.edit();
		edt.putLong("version_reference_" + version, reference);
		edt.putString("latestVersion", version);
		edt.putString("version_downLoad", filePath);
		edt.commit();

	}

	public static long getDownLoadReference(Context context) {
		SharedPreferences sp = getSharedPreferences(context);
		String version = sp.getString("latestVersion", "");
		return sp.getLong("version_reference_" + version, 0);
	}

	public static String getlatestVersionPath(Context context) {
		SharedPreferences sp = getSharedPreferences(context);
		String path = sp.getString("version_downLoad", "");
		return path;
	}

	public static void saveJson(Context context, String key, String value) {
		SharedPreferences sp = getSharedPreferences(context);
		sp.edit().putString(key, value).commit();
	}

	public static String getJson(Context context, String key) {
		SharedPreferences sp = getSharedPreferences(context);
		String path = sp.getString(key, "");

		return path;
	}




	/**
	 * 获取application中指定的meta-data
	 * @return 如果没有获取成功(没有对应值，或者异常)，则返回值为空
	 */
	public static String getAppMetaData(Context ctx, String key) {
		if (ctx == null || key.equals("")) {
			return null;
		}
		String resultData = null;
		try {
			PackageManager packageManager = ctx.getPackageManager();
			if (packageManager != null) {
				ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
				if (applicationInfo != null) {
					if (applicationInfo.metaData != null) {
						resultData = applicationInfo.metaData.getString(key);
					}
				}

			}
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}

		return resultData;
	}

	public static String getCurrentAppVersion(Context context) {
		PackageManager pm = context.getPackageManager();
		try {
			String appVersionName = pm.getPackageInfo(context.getPackageName(), 0).versionName;
			return appVersionName;
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		return "2.0.2";
	}

	public static void saveAesKey(Context context, String key) {
		SharedPreferences sp = getSharedPreferences(context);
		sp.edit().putString("aeskey", key).commit();
	}

	public static String getAesKey(Context context) {
		SharedPreferences sp = getSharedPreferences(context);
		return sp.getString("aeskey", "");
	}

	public static void saveAesIv(Context context, String key) {
		SharedPreferences sp = getSharedPreferences(context);
		sp.edit().putString("aesiv", key).commit();
	}

	public static String getAesIv(Context context) {
		SharedPreferences sp = getSharedPreferences(context);
		return sp.getString("aesiv", "");
	}

	public static String getWebRootDir(Context context) {
		SharedPreferences sp = getSharedPreferences(context);
		return sp.getString("webrootdir", context.getDir("web",Context.MODE_PRIVATE).getAbsolutePath() + File.separator + PackegeConfig.WEB_VERSION);
	}

	public static void saveWebRootDir(Context context, String dir) {
		SharedPreferences sp = getSharedPreferences(context);
		sp.edit().putString("webrootdir", dir).commit();
	}

	public static void saveWebVersion(Context context, String version) {
		SharedPreferences sp = getSharedPreferences(context);
		sp.edit().putString("webversion", version).commit();
	}

	public static String getWebVersion(Context context) {
		SharedPreferences sp = getSharedPreferences(context);
		return sp.getString("webversion", PackegeConfig.WEB_VERSION);
	}

	public static void saveWebUnzipStatus(Context context, boolean status) {
		SharedPreferences sp = getSharedPreferences(context);
		sp.edit().putBoolean("webUnzipStatus", status).commit();
	}

	public static boolean getWebUnzipStatus(Context context) {
		SharedPreferences sp = getSharedPreferences(context);
		return sp.getBoolean("webUnzipStatus", false);
	}

}
